package com.aidex.biz.service.impl;

import com.aidex.biz.domain.UserInfo;
import com.aidex.biz.mapper.UserInfoMapper;
import com.aidex.biz.service.UserInfoService;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.core.domain.entity.Company;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.utils.NumberUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.http.HttpUtils;
import com.aidex.system.service.CompanyService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小程序用户信息Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-09-21
 */
@Service
@Transactional(readOnly = true)
public class UserInfoServiceImpl extends BaseServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    private static final Logger log = LoggerFactory.getLogger(UserInfoServiceImpl.class);

    @Autowired
    private CompanyService companyService;

    /**
     * 获取单条数据
     *
     * @param userInfo 小程序用户信息
     * @return 小程序用户信息
     */
    @Override
    public UserInfo get(UserInfo userInfo) {
        UserInfo dto = super.get(userInfo);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 小程序用户信息id
     * @return 小程序用户信息
     */
    @Override
    public UserInfo get(String id) {
        UserInfo dto = super.get(id);
        return dto;
    }


    /**
     * 查询小程序用户信息列表
     *
     * @param userInfo 小程序用户信息
     * @return 小程序用户信息
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public List<UserInfo> findList(UserInfo userInfo) {
        return super.findList(userInfo);
    }

    /**
     * 分页查询小程序用户信息列表
     *
     * @param userInfo 小程序用户信息
     * @return 小程序用户信息
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public PageInfo<UserInfo> findPage(UserInfo userInfo) {
        return super.findPage(userInfo);
    }

    /**
     * 保存小程序用户信息
     *
     * @param userInfo
     * @return 结果
     */
    @Override
    public boolean save(UserInfo userInfo) {
        return super.save(userInfo);
    }


    /**
     * 删除小程序用户信息信息
     *
     * @param userInfo
     * @return 结果
     */
    @Override
    public boolean remove(UserInfo userInfo) {
        return super.remove(userInfo);
    }

    /**
     * 批量删除小程序用户信息
     *
     * @param ids 需要删除的小程序用户信息ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteUserInfoByIds(String[] ids) {
        return mapper.deleteUserInfoByIds(ids);
    }

    /**
     * 获取最大编号
     *
     * @param userInfo 小程序用户信息
     * @return 结果
     */
    @Override
    public int findMaxSort(UserInfo userInfo) {
        return NumberUtils.nextOrder(mapper.findMaxSort(userInfo));
    }

    /**
     * 根据手机查询
     *
     * @param userInfo
     * @return
     */
    @Override
    public List<UserInfo> findOneByPhone(UserInfo userInfo) {
        return mapper.findOneByPhone(userInfo);
    }

    @Override
    public List<UserInfo> findListTop20(UserInfo userInfo) {
        return mapper.findListTop20(userInfo);
    }

    /**
     * 根据手机查询姓名查询
     *
     * @param userInfo
     * @return
     */
    public UserInfo getOne(UserInfo userInfo) {
        return mapper.getOne(userInfo);
    }

    @Override
    public boolean toShopAddUser(UserInfo userInfo) {
        //
        Company company = companyService.get(userInfo.getCompanyId());

        String path = company.getShopUrl();

        Map<String, String> map = new HashMap<>();
        map.put("appid", company.getShopAppId());
        map.put("appsecret", company.getShopAppKey());
        String result = HttpUtils.doPost(path + "/access_token", map);
        if (StringUtils.isEmpty(result)) {
            return false;
        }
        JSONObject resultjson = JSONObject.parseObject(result);
        if (resultjson.getInteger("status") == 200) {
            JSONObject datajson = JSONObject.parseObject(resultjson.getString("data"));
            String token = datajson.getString("access_token");
            map = new HashMap<>();
            map.put("real_name", userInfo.getName());
            map.put("phone", userInfo.getPhone());
            result = HttpUtils.doPost(path + "/user", map, token);
            if (StringUtils.isEmpty(result)) {
                return false;
            } else {
                resultjson = JSONObject.parseObject(result);
                if (resultjson.getInteger("status") == 200) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public boolean shopUserUpLevel(UserInfo userInfo) {
        Company company = companyService.get(userInfo.getCompanyId());
        String path = company.getShopUrl();
        Map<String, String> map = new HashMap<>();
        map.put("appid", company.getShopAppId());
        map.put("appsecret", company.getShopAppKey());
        String result = HttpUtils.doPost(path + "/access_token", map);
        if (StringUtils.isEmpty(result)) {
            return false;
        }
        JSONObject resultjson = JSONObject.parseObject(result);
        if (resultjson.getInteger("status") == 200) {
            JSONObject datajson = JSONObject.parseObject(resultjson.getString("data"));
            String token = datajson.getString("access_token");
            map = new HashMap<>();
            result = HttpUtils.doPost(path + "/user/update_level/" + userInfo.getPhone(), map, token);
            if (StringUtils.isEmpty(result)) {
                return false;
            } else {
                resultjson = JSONObject.parseObject(result);
                if (resultjson.getInteger("status") == 200) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 根据手机查询
     *
     * @param
     * @return
     */
    public UserInfo findOneByPhone(String phone, String companyId) {
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setCompanyId(companyId);
        return mapper.findOneByPhone(userInfo).size() > 0 ? mapper.findOneByPhone(userInfo).get(0) : null;
    }

    public List<Map<String, Object>> getRepUserInfo() {
        return mapper.getRepUserInfo();
    }
}
